package com.xintonghua.bussiness.ui.fragment.client.user;


import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xintonghua.base.utils.AllActivitiesHolder;
import com.xintonghua.base.widget.ConfirmDialog;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.ClubCardBean;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.bean.RechargeViewBean;
import com.xintonghua.bussiness.bean.UserInfoBean;
import com.xintonghua.bussiness.ui.user.LoginActivity;
import com.xintonghua.bussiness.ui.user.SettingActivity;
import com.xintonghua.bussiness.util.AbstractBaseAdapter;
import com.xintonghua.bussiness.util.GlideUtils;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MathUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;
import com.xintonghua.bussiness.util.RegexUtils;
import com.xintonghua.bussiness.util.ViewHolder;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by liqi on 2018/3/31.
 * 为会员卡充值
 */

public class RechargeMemberCardActivity extends BaseActivity {

    Contant contantbean;//上上个页面的，用户信息

    ClubCardBean.ListBean bean;//上个页面的  会员卡信息
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_all_jinger)
    TextView tvAllJinger;
    @BindView(R.id.tv_jinger)
    TextView tvJinger;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.lv_list)
    ListView lvList;

    UserInfoBean uupUserInfoBean;//上上个页面的，主要用于获取头像

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rechargemember_card);
        ButterKnife.bind(this);
        contantbean = (Contant) JumpUtils.getSerializable(this, "data0");
        bean = (ClubCardBean.ListBean) JumpUtils.getSerializable(this, "data1");
        uupUserInfoBean = (UserInfoBean) JumpUtils.getSerializable(this, "data2");
        onSimpleActionBar();
        setSimpleActionBar("充值");
        initUI();
        getNetData();
    }

    RechargeViewBean.UserInfoBean list_UserData;
    List<RechargeViewBean.MemberCardsBean> list_member_card;
    int SelectPostion = 0;
    BaseAdapter adapter;

    @Override
    public void initUI() {
        super.initUI();
        list_member_card = new ArrayList<>();

        lvList.setAdapter(adapter = new AbstractBaseAdapter<RechargeViewBean.MemberCardsBean>(list_member_card) {
            @Override
            public View getAdapterViewAtPosition(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(RechargeMemberCardActivity.this).inflate(R.layout.item_rechange, null);
                }
                RechargeViewBean.MemberCardsBean bean = getItem(position);
                TextView tv_titles = ViewHolder.get(convertView, R.id.tv_titles);
                ImageView iv_gou = ViewHolder.get(convertView, R.id.iv_gou);
                tv_titles.setText("" + bean.getTitle());

                if (SelectPostion == position) {
                    iv_gou.setSelected(true);
                } else {
                    iv_gou.setSelected(false);
                }

                return convertView;
            }
        });
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                SelectPostion = i;
                jisuanjiage();
                adapter.notifyDataSetChanged();
            }
        });
    }

    RechargeViewBean rechargeViewBean;

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestOne:
                    rechargeViewBean = JsonUtil.getEntityByJsonString(data.toString(), RechargeViewBean.class);
                    initView(rechargeViewBean);
                    break;
                case MyState.requestTwo:
                    mToast("充值成功");
                    finish();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("aaa", "出错了");
        }
    }

    public void jisuanjiage() {
//        String max = "0";
//        List<String> alist = RegexUtils.getCustomRegex("" + rechargeViewBean.getMemberCards().get(SelectPostion).getTitle(), "[0-9]+");
//
//        for (int i = 0; i < alist.size() - 1; i++) {
//            if (MathUtils.strcompareTo(alist.get(i), max)) {
//                max = alist.get(i);
//            }
//        }
//        tvJinger.setText("¥" + max);
        if (rechargeViewBean.getMemberCards().size()>0){
            tvJinger.setText("¥" + MyUtils.getMoneys(this,rechargeViewBean.getMemberCards().get(0).getAmount()));
        }else {
            tvJinger.setText("¥0.00");
        }

    }


    public void initView(RechargeViewBean rechargeViewBean) {
        list_member_card.clear();
        list_member_card.addAll(rechargeViewBean.getMemberCards());
        adapter.notifyDataSetChanged();
        tvName.setText("" + rechargeViewBean.getUserInfo().getNickname());
        tvPhone.setText("" + rechargeViewBean.getUserInfo().getPhone());
        if (rechargeViewBean.getMemberCards().size()>0){
            tvJinger.setText("¥" + MyUtils.getMoneys(this,rechargeViewBean.getMemberCards().get(0).getAmount()));
        }else {
            tvJinger.setText("¥0.00");
        }

       // List<String> list = RegexUtils.getCustomRegex("" + rechargeViewBean.getMemberCards().get(SelectPostion).getTitle(), "[0-9]+");

//        String max = "0";
//        for (int i = 0; i < list.size() - 1; i++) {
//            if (MathUtils.strcompareTo(list.get(i), max)) {
//                max = list.get(i);
//            }
//        }



        GlideUtils.load(RechargeMemberCardActivity.this, ivHead, "" + uupUserInfoBean.getHead_img());
        //1990-03-10 00:00:00
        String[] str = uupUserInfoBean.getBirth().split(" ");
        String riqi = str[0].replaceAll("-", "/");
        String birth = riqi;


    }

    //String customberId, String memberCardTypeId,
    public void getNetData() {
        HttpCent.getInstance(this).getMemberRechargeTypes("" + contantbean.getId(), "" + bean.getTypeId(), this, MyState.requestOne);
    }

    public void Rechange() {
        HttpCent.getInstance(this).RechangeMemberCard("" + contantbean.getId(), "" + bean.getId(), "" + list_member_card.get(SelectPostion).getId(), bean.getTypeId(), this, MyState.requestTwo);
    }

    @OnClick(R.id.btn_ok)
    public void onViewClicked() {
        if (list_member_card == null || list_member_card.size() == 0) {
            mToast("请选择充值选项");
            return;
        }

        ConfirmDialog.showDialog(this, "提示", "是否确定充值？", "取消", "确定", null, new ConfirmDialog.OnRightListener() {
            @Override
            public void onClick(ConfirmDialog dialog) {

                Rechange();
            }
        });
    }
}
